<?php

/**
 * This is the model class for table "categorys".
 *
 * The followings are the available columns in table 'categorys':
 * @property string $id
 * @property string $category_parent
 * @property string $category_model
 * @property string $category_type
 * @property string $category_name
 * @property string $category_alias
 * @property string $category_content
 * @property string $seo_title
 * @property string $seo_keywords
 * @property string $seo_description
 * @property integer $category_status
 * @property integer $category_ismenu
 */
class Categorys extends CActiveRecord {

    public $category_model_array = array(
        'article' => '文档',
        'album' => '相册',
        'download' => '下载',
        'product' => '产品',
        'faq' => '问答'
    );
    public $category_type_array = array(
        'page' => '单页面',
        'link' => '链接',
        'list' => '列表页',
        'index' => '封面页',
        'faq' => '问答首页'
    );
    public $status_array = array(
        0 => '禁用',
        1 => '启用'
    );
    public $menu_array = array(
        0 => '否',
        1 => '是'
    );

    const STATUS_ALLOW = 1;
    const STATUS_DENY = 0;
    const ISMENU_YES = 1;
    const ISMENU_NO = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Categorys the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{categorys}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('category_name, category_alias,category_model,category_type', 'required'),
            array('category_status, category_ismenu, category_order', 'numerical', 'integerOnly' => true),
            array('category_alias', 'unique'),
            array('category_model', 'length', 'max' => 8),
            array('category_type', 'length', 'max' => 5),
            array('category_name, category_alias, seo_title, seo_keywords', 'length', 'max' => 255),
            array('category_content, seo_description, category_parent, category_image, category_intro', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, category_parent, category_model, category_type, category_name, category_alias, category_content, seo_title, seo_keywords, seo_description, category_status, category_ismenu', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'category_parent' => '上级分类',
            'category_model' => '栏目模型',
            'category_type' => '栏目类型',
            'category_name' => '栏目名称',
            'category_alias' => '别名（用于URL）',
            'category_content' => '栏目详细内容',
            'seo_title' => 'SEO 标题',
            'seo_keywords' => 'SEO 关键字',
            'seo_description' => 'SEO 描述',
            'category_status' => '状态',
            'category_ismenu' => '导航显示',
            'category_order' => '排序',
            'category_image' => '栏目图片',
            'category_intro' => '简介'
        );
    }

    /**
     * 获取上级栏目名称
     * @param type $id
     * @return null 
     */
    public function parentName($id) {
        $id = (int) $id;
        if ($id <= 0) {
            return null;
        } else {
            $data = $this->findByPk($id);
            if ($data) {
                return $data->category_name;
            } else {
                return null;
            }
        }
    }

    /**
     * 获取栏目列表，主从栏目已分好
     * @return array
     */
    public function getCategorysArray() {
        $cr = new CDbCriteria;
        $cr->select = 'id, category_parent, category_model, category_type, category_name, category_alias,  category_status, category_ismenu';
        $categorys = array();
        foreach ($this->findAll($cr) as $c) {
            if ($c['category_parent'] == 0) {
                $categorys[$c['id']] = $c->getAttributes();
            } else {
                $categorys[$c['category_parent']]['subs'][] = $c->getAttributes();
            }
        }
        return $categorys;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('category_parent', $this->category_parent);
        $criteria->compare('category_model', $this->category_model);
        $criteria->compare('category_type', $this->category_type);
        $criteria->compare('category_name', $this->category_name, true);
        $criteria->compare('category_alias', $this->category_alias);
        $criteria->compare('category_status', $this->category_status);
        $criteria->compare('category_ismenu', $this->category_ismenu);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'category_parent ASC'
                    ),
                    'pagination' => array(
                        'pageSize' => 30
                    ),
                ));
    }

    /**
     * 导航生成
     * @return string 
     */
    public function topMenu() {
        $cr = new CDbCriteria;
        $cr->compare('category_status', 1);
        $cr->compare('category_parent', 0);
        $cr->compare('category_ismenu', 1);
        $cr->order = 'category_order DESC';
        $data = $this->findAll($cr);

        $css = "selected";
        $controllerId = Yii::app()->controller->id;
        $actionId = Yii::app()->controller->action->id;
        $homeCss = '';
        if ($controllerId == 'site' && $actionId == 'index')
            $homeCss = $css;
        $menuStr = "<li>" . CHtml::link('首页', Yii::app()->createAbsoluteUrl('/'), array('title' => Yii::app()->name, 'class' => $homeCss)) . "</li>";

        foreach ($data as $v) {
            $link = '';
            $categoryCss = '';
            if (!empty($_GET['alias'])) {
                $mData = $this->findByAttributes(array('category_alias' => $_GET['alias']));
                if ($controllerId == 'categorys' && ($_GET['alias'] == $v->category_alias || $mData->category_parent == $v->id)) {
                    $categoryCss = $css;
                }
            }

            switch ($v->category_type) {
                case 'page':
                    $link = CHtml::link($v->category_name, Yii::app()->createAbsoluteUrl('categorys/view', array('alias' => $v->category_alias)), array('class' => $categoryCss));
                    break;
                case 'faq':
                    $link = CHtml::link($v->category_name, Yii::app()->createAbsoluteUrl('categorys/faq', array('alias' => $v->category_alias)), array('class' => $categoryCss));
                    break;
                case 'list':
                default:
                    $link = CHtml::link($v->category_name, Yii::app()->createAbsoluteUrl('categorys/list', array('alias' => $v->category_alias)), array('class' => $categoryCss));
            }

            $cr = new CDbCriteria;
            $cr->compare('category_status', 1);
            $cr->compare('category_parent', $v->id);
            $cr->compare('category_ismenu', 1);
            $cr->order = 'category_order DESC';
            $sdata = $this->findAll($cr);
            $sMenu = '';
            if ($sdata) {
                foreach ($sdata as $sv) {
                    $slink = CHtml::link($sv->category_name, $sv->createCategoryUrl());
                    $sMenu .= "<li>$slink</li>";
                }
                $sMenu = "<ul>$sMenu</ul>";
            }
            $menuStr .= "<li>{$link}{$sMenu}</li>";
        }
        return $menuStr;
    }

    /**
     * 获取子栏目
     * @param type $parendId
     * @return type
     */
    public function getSubCategorys($parendId) {
        $cr = new CDbCriteria;
        $cr->compare('category_parent', $parendId);
        $cr->order = 'category_order DESC';
        return $this->findAll($cr);
    }

    /**
     * 生成栏目的URL
     * @return string
     */
    public function createCategoryUrl() {
        switch ($this->category_type) {
            case 'page':
                return Yii::app()->createAbsoluteUrl('categorys/view', array('alias' => $this->category_alias));
                break;
            case 'faq':
                return Yii::app()->createAbsoluteUrl('categorys/faq', array('alias' => $this->category_alias));
                break;
            case 'list':
            default:
                return Yii::app()->createAbsoluteUrl('categorys/list', array('alias' => $this->category_alias));
                break;
        }
    }

}